package com.gip.service.Impl;

import com.gip.dto.RecordInfo;
import com.gip.mapper.AuctionCustomerMapper;
import com.gip.mapper.AuctionMapper;
import com.gip.mapper.AuctionrecordMapper;
import com.gip.mapper.RecordMapper;
import com.gip.pojo.*;
import com.gip.service.AuctionService;
import com.gip.utils.AuctionPriceException;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional
public class AuctionServiceImpl implements AuctionService {

    @Autowired
    AuctionMapper auctionMapper;

    @Autowired
    RecordMapper recordMapper;

    @Autowired
    AuctionCustomerMapper auctionCustomerMapper;

    @Autowired
    AuctionrecordMapper auctionrecordMapper;


    @Override
    public List<Auction> queryAllAuctions() {
//        AuctionExample example = new AuctionExample();
//        AuctionExample.Criteria criteria = example.createCriteria();
//        List<Auction> auctionList = this.auctionMapper.selectByExample(example);
        List<Auction> auctionList = this.auctionMapper.selectByExample(null);
        return auctionList;
    }

    @Override
    public List<Auction> queryAllAuctions(Auction auction) {
        AuctionExample example = new AuctionExample();
        AuctionExample.Criteria criteria = example.createCriteria();
        if (auction != null) {
//            至少有一个输入条件
            if (auction.getAuctionname() != null && auction.getAuctionname().length() != 0) {
//                拍卖物品名称不为空
                criteria.andAuctionnameLike("%" + auction.getAuctionname() + "%");
            }
            if (auction.getAuctiondesc() != null && !auction.getAuctiondesc().isEmpty()) {
//                拍卖物描述不为空
                criteria.andAuctiondescEqualTo(auction.getAuctiondesc());
            }
            if (auction.getAuctionstarttime() != null) {
//                大于拍卖开始时间
                criteria.andAuctionstarttimeGreaterThan(auction.getAuctionstarttime());
            }
            if (auction.getAuctionendtime() != null) {
//                小于拍卖时间
                criteria.andAuctionendtimeLessThan(auction.getAuctionendtime());
            }
            if (auction.getAuctionstartprice() != null) {
//                大于起拍价
                criteria.andAuctionstartpriceGreaterThan(auction.getAuctionstartprice());
            }
//            根据开始时间进行降序排序
            example.setOrderByClause("auctionstarttime desc");
        }
        List<Auction> auctionList = this.auctionMapper.selectByExample(example);
        return auctionList;
    }

    @Override
    public List<Auction> queryAllAuctions(Auction auction, int page, int size) {

        AuctionExample example = new AuctionExample();
        AuctionExample.Criteria criteria = example.createCriteria();

        if (auction != null) {
//            至少有一个输入条件
            if (auction.getAuctionname() != null && auction.getAuctionname().length() != 0) {
//                拍卖物品名称不为空
                criteria.andAuctionnameLike("%" + auction.getAuctionname() + "%");
            }
            if (auction.getAuctiondesc() != null && !auction.getAuctiondesc().isEmpty()) {
//                拍卖物描述不为空
                criteria.andAuctiondescEqualTo(auction.getAuctiondesc());
            }
            if (auction.getAuctionstarttime() != null) {
//                大于拍卖开始时间
                criteria.andAuctionstarttimeGreaterThan(auction.getAuctionstarttime());
            }
            if (auction.getAuctionendtime() != null) {
//                小于拍卖时间
                criteria.andAuctionendtimeLessThan(auction.getAuctionendtime());
            }
            if (auction.getAuctionstartprice() != null) {
//                大于起拍价
                criteria.andAuctionstartpriceGreaterThan(auction.getAuctionstartprice());

            }
//            根据开始时间进行降序排序
            example.setOrderByClause("auctionstarttime desc");
        }
//        分页
        PageHelper.startPage(page, size);
//        查询
        List<Auction> auctionList = this.auctionMapper.selectByExample(example);

        return auctionList;
    }

    @Override
    public List<RecordInfo> getRecords(int id) {
        List<RecordInfo> record = recordMapper.getRecord(id);
        return record;
    }

    @Override
    public Auction selectAuctionDetailByAuctionId(int id) {
        return auctionCustomerMapper.selectAuctionDetailByAuctionId(id);
    }

    @Override
    public void saveAuctionRecord(Auctionrecord auctionrecord) throws AuctionPriceException {
        //  Auction auction = this.auctionMapper.selectByPrimaryKey(auctionrecord.getAuctionid());

        Auction auction = this.auctionCustomerMapper.selectAuctionAndAuctionRecordList(auctionrecord.getAuctionid());
        // 判断结束时间，不能过期
        if (auction.getAuctionendtime().after(new Date()) == false) {
            throw new AuctionPriceException("当前竞拍活动已经结束了");
        }
        // 判断价格：
        if (auction.getAuctionrecodList().size() > 0) {
            // 当前是有竞拍纪录的
            // 取出所有竞拍纪录中的最高价
            Auctionrecord maxRecord = auction.getAuctionrecodList().get(0);
            // compareTo  比较 BigDeimal   0 1  -1
            if (auctionrecord.getAuctionprice().compareTo(maxRecord.getAuctionprice()) < 1) {
                throw new AuctionPriceException("您出的价格不能低于最高价");
            }
        }
        //如果商品从未竞价，价格必须高于起拍价
        else {
            if (auctionrecord.getAuctionprice().compareTo(auction.getAuctionstartprice()) < 1) {
                throw new AuctionPriceException("您出的价格不能低于起拍价");
            }
        }

        this.auctionrecordMapper.insert(auctionrecord);
    }

    @Override
    public List<AuctionCustomer> selectAuctionendtime() {
        return this.auctionCustomerMapper.selectAuctionendtime();
    }

    @Override
    public List<Auction> selectAuctionNoendtime() {
        return this.auctionCustomerMapper.selectAuctionNoendtime();
    }

    @Override
    public void addAuction(Auction auction) {
        this.auctionMapper.insert(auction);
    }

    @Override
    public int updateAuction(Auction auction) {
        return auctionMapper.updateByPrimaryKeySelective(auction);
    }

    @Override
    public int deleteByAuctionid(int id) {
        AuctionrecordExample auctionrecordExample = new AuctionrecordExample();
        AuctionrecordExample.Criteria criteria = auctionrecordExample.createCriteria();
        criteria.andAuctionidEqualTo(id);
        int i = auctionrecordMapper.deleteByExample(auctionrecordExample);
        int i1 = auctionMapper.deleteByPrimaryKey(id);
        if(i1>=1){
            return 1;
        }else{
            return 0;
        }

    }
}
